package cn.sczc.jh.client.controller.mapimage;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.sczc.jh.client.base.AbstractController;
import cn.sczc.jh.client.entity.MapImage;
import cn.sczc.jh.client.service.mapimage.MapImageService;
import cn.sczc.jh.client.vo.MapImageVo;
import cn.sczc.jh.client.vo.UploadEntity;
import cn.sczc.jh.client.websocket.encoder.WebSocketMessageEncoder;

@RestController
@RequestMapping("/mapimages")
public class MapImageController extends AbstractController{
	private static final Logger log = LogManager.getLogger(WebSocketMessageEncoder.class);

	@Autowired
	MapImageService mapImageService;

	// 项目根路径下的目录 -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
	public final static String UPLOAD_PATH_PREFIX = "src/main/resources/static/uploadImage/";

	@RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> uploadImg(@RequestParam(value = "file", required = true) MultipartFile file,
			HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		if (!file.isEmpty()) {
			try {
				String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				String fid = UUID.randomUUID().toString().replace("-", "");
				ApplicationHome home = new ApplicationHome(getClass());
			    File jarFile = home.getSource();
			    String uploadPath = jarFile.getParentFile().toString()+ File.separator + "uploads"
						+ File.separator + "images" + File.separator;
				log.info("uploadPath==={}" + uploadPath);
				String dbFilePath = File.separator+"uploads"+File.separator+"images"+File.separator+ fid + suffix;
				String filePath = uploadPath + fid + suffix;
				File f = new File(uploadPath);
				if (!f.exists()) {
					f.mkdirs();
				}
				file.transferTo(new File(filePath));

				UploadEntity result = new UploadEntity();
				result.setFid(dbFilePath);
				result.setName(file.getOriginalFilename());
				result.setUrl(dbFilePath);
				result.setSuccess(true);
				return setSuccessModelMap(modelMap,result);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> list(ModelMap modelMap,@RequestParam Map<String, Object> params) {
		List<MapImage> data = mapImageService.getMapImages(params);
		return setSuccessModelMap(modelMap,data);
	}

	/**
	 * 添加地图图片
	 * 
	 * @param mapIamge
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> add(ModelMap modelMap,@RequestBody Map<String, Object> params) {
		MapImage mapImage = mapImageService.addMapImage(params);
		return setSuccessModelMap(modelMap,mapImage);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ModelMap> del(ModelMap modelMap,@PathVariable String id) {
		int count = mapImageService.delById(id);
		return setSuccessModelMap(modelMap,count);
	}

	/**
	 * 修改
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public ResponseEntity<ModelMap> update(ModelMap modelMap, @RequestBody List<MapImageVo> params) {
		List<MapImage> list = mapImageService.updateMapImage(params);
		return setSuccessModelMap(modelMap,list);
	}

}
